package cumt.kydd.authoringplatform.service;


import cumt.kydd.authoringplatform.mapper.HistoryMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import static cumt.kydd.authoringplatform.service.UserService.LoginInUser;


@Service
public class FavorHistoryService {

    @Resource
    private HistoryMapper historyMapper;

    public List<Integer> getUsersHistoryID()
    {
        List<Integer> result = new LinkedList<>();
        result = historyMapper.selectHistoryIDByNameAndIsDelete(LoginInUser.getName());

        return result;
    }

    public List<Integer> getUsersFavorHistoryID()
    {
        List<Integer> result = new LinkedList<>();
        result = historyMapper.selectHistoryIDByNameAndIsFavor(LoginInUser.getName());

        return result;
    }

    public Map<String,Object> favorHistory(int is_favor,int flag_now_his){
        Map<String,Object> resultMap=new HashMap<>();
        List<Integer> IDList = getUsersHistoryID();
        int flag_now_id = IDList.get(flag_now_his);
        if (historyMapper.selectHistoryByID(flag_now_id).getIsFavor() == 0) {
            int result = historyMapper.updateIsFavor(is_favor,flag_now_id);
            if(result>0)
            {
                resultMap.put("code",200);
                resultMap.put("message","收藏成功！");
            }
            else
            {
                resultMap.put("code",400);
                resultMap.put("message","未知错误，收藏失败!");
            }
        } else {
            resultMap.put("code",400);
            resultMap.put("message","收藏失败，该条目已经被收藏！");
        }
        return resultMap;
    }

    public Map<String,Object> cancelFavorHistory(int is_favor,int flag_now_his){

        List<Integer> IDList = getUsersFavorHistoryID();
        int flag_now_id = IDList.get(flag_now_his);
        System.out.println("要取消收藏的id "+flag_now_id);
        int result = historyMapper.updateIsFavor(is_favor,flag_now_id);
        Map<String,Object> resultMap=new HashMap<>();
        if(result>0)
        {
            resultMap.put("code",200);
            resultMap.put("message","取消收藏成功！");
        }
        else
        {
            resultMap.put("code",400);
            resultMap.put("message","未知错误，取消收藏失败!");
        }
        return resultMap;
    }
}
